using ClassDemo.Model.Entity;
using SqlSugar;

namespace ClassDemo.Repository;

/// <summary>
/// 教师Repository实现
/// </summary>
public class TeacherRepository : BaseRepository<Teacher>, ITeacherRepository
{
    public TeacherRepository(SqlSugarClient db) : base(db)
    {
    }

    /// <summary>
    /// 根据姓名查询教师列表
    /// </summary>
    /// <param name="name">教师姓名</param>
    /// <returns>教师列表</returns>
    public async Task<List<Teacher>> GetByNameAsync(string name)
    {
        return await _db.Queryable<Teacher>()
            .Where(t => t.Name.Contains(name))
            .ToListAsync();
    }

    /// <summary>
    /// 根据手机号查询教师
    /// </summary>
    /// <param name="mobile">手机号</param>
    /// <returns>教师信息</returns>
    public async Task<Teacher?> GetByMobileAsync(string mobile)
    {
        return await _db.Queryable<Teacher>()
            .Where(t => t.Mobile == mobile)
            .FirstAsync();
    }

    /// <summary>
    /// 检查手机号是否已存在
    /// </summary>
    /// <param name="mobile">手机号</param>
    /// <param name="excludeId">排除的教师ID（用于更新时检查）</param>
    /// <returns>是否存在</returns>
    public async Task<bool> IsMobileExistsAsync(string mobile, int? excludeId = null)
    {
        var query = _db.Queryable<Teacher>().Where(t => t.Mobile == mobile);
        
        if (excludeId.HasValue)
        {
            query = query.Where(t => t.ID != excludeId.Value);
        }
        
        return await query.AnyAsync();
    }
}